<template>
  <div class="teacher-center">
    <div class="page-header">
      <h1 class="page-title">教师交流中心</h1>
      <div class="search-box">
        <el-input
          v-model="searchQuery"
          placeholder="搜索教师姓名或专业领域"
          prefix-icon="el-icon-search"
          @input="handleSearch"
        />
      </div>
    </div>

    <div class="filter-section">
      <div class="filter-item">
        <span class="filter-label">学科:</span>
        <el-radio-group v-model="filters.subject" @change="handleFilterChange">
          <el-radio label="">全部</el-radio>
          <el-radio label="math">数学</el-radio>
          <el-radio label="english">英语</el-radio>
          <el-radio label="physics">物理</el-radio>
          <el-radio label="chemistry">化学</el-radio>
          <el-radio label="biology">生物</el-radio>
        </el-radio-group>
      </div>
      
      <div class="filter-item">
        <span class="filter-label">职称:</span>
        <el-radio-group v-model="filters.title" @change="handleFilterChange">
          <el-radio label="">全部</el-radio>
          <el-radio label="professor">教授</el-radio>
          <el-radio label="associate_professor">副教授</el-radio>
          <el-radio label="lecturer">讲师</el-radio>
          <el-radio label="assistant">助教</el-radio>
        </el-radio-group>
      </div>
    </div>

    <div class="teacher-list" v-loading="loading">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="teacher in teachers" :key="teacher.id">
          <router-link :to="`/teacher-center/${teacher.id}`" class="teacher-card">
            <div class="teacher-avatar">
              <img :src="teacher.avatar" :alt="teacher.name">
              <div class="teacher-title">{{ getTitleText(teacher.title) }}</div>
            </div>
            <div class="teacher-info">
              <h3 class="teacher-name">{{ teacher.name }}</h3>
              <div class="teacher-subject">{{ getSubjectText(teacher.subject) }}</div>
              <div class="teacher-stats">
                <div class="stat-item">
                  <i class="el-icon-reading"></i>
                  <span>{{ teacher.courseCount }}门课程</span>
                </div>
                <div class="stat-item">
                  <i class="el-icon-user"></i>
                  <span>{{ teacher.studentCount }}名学生</span>
                </div>
              </div>
              <div class="teacher-rating">
                <el-rate
                  v-model="teacher.rating"
                  disabled
                  text-color="#ff9900"
                  score-template="{value}"
                />
                <span class="rating-value">{{ teacher.rating.toFixed(1) }}</span>
              </div>
              <div class="teacher-intro">{{ teacher.introduction.substring(0, 50) }}{{ teacher.introduction.length > 50 ? '...' : '' }}</div>
            </div>
          </router-link>
        </el-col>
      </el-row>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalTeachers"
        :page-size="pageSize"
        :current-page.sync="currentPage"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script>
import api from '@/api'

export default {
  name: 'TeacherCenter',
  data() {
    return {
      loading: false,
      searchQuery: '',
      teachers: [],
      totalTeachers: 0,
      currentPage: 1,
      pageSize: 12,
      filters: {
        subject: '',
        title: ''
      }
    }
  },
  created() {
    this.fetchTeachers()
  },
  methods: {
    async fetchTeachers() {
      this.loading = true
      try {
        const params = {
          page: this.currentPage,
          limit: this.pageSize,
          subject: this.filters.subject,
          title: this.filters.title,
          query: this.searchQuery
        }
        
        const response = await api.teacher.getTeacherList(params)
        this.teachers = response.data.items
        this.totalTeachers = response.data.total
      } catch (error) {
        console.error('获取教师列表失败:', error)
        this.$message.error('获取教师列表失败，请稍后重试')
      } finally {
        this.loading = false
      }
    },
    handleSearch() {
      this.currentPage = 1
      this.fetchTeachers()
    },
    handleFilterChange() {
      this.currentPage = 1
      this.fetchTeachers()
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchTeachers()
    },
    getSubjectText(subject) {
      const subjectMap = {
        math: '数学',
        english: '英语',
        physics: '物理',
        chemistry: '化学',
        biology: '生物'
      }
      return subjectMap[subject] || '综合'
    },
    getTitleText(title) {
      const titleMap = {
        professor: '教授',
        associate_professor: '副教授',
        lecturer: '讲师',
        assistant: '助教'
      }
      return titleMap[title] || '教师'
    }
  }
}
</script>

<style lang="scss" scoped>
.teacher-center {
  padding: 20px;

  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .page-title {
      font-size: 24px;
      font-weight: bold;
      color: #333;
      margin: 0;
    }

    .search-box {
      width: 300px;
    }
  }

  .filter-section {
    background-color: #f5f7fa;
    padding: 15px;
    border-radius: 4px;
    margin-bottom: 20px;

    .filter-item {
      margin-bottom: 10px;
      
      &:last-child {
        margin-bottom: 0;
      }

      .filter-label {
        font-weight: bold;
        margin-right: 10px;
        color: #606266;
      }
    }
  }

  .teacher-list {
    margin-bottom: 20px;
    min-height: 400px;
  }

  .teacher-card {
    display: block;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    overflow: hidden;
    margin-bottom: 20px;
    transition: transform 0.3s;
    text-decoration: none;
    color: inherit;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    .teacher-avatar {
      position: relative;
      height: 180px;
      overflow: hidden;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .teacher-title {
        position: absolute;
        bottom: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.6);
        color: #fff;
        padding: 4px 8px;
        font-size: 12px;
        border-top-left-radius: 4px;
      }
    }

    .teacher-info {
      padding: 15px;

      .teacher-name {
        margin: 0 0 5px;
        font-size: 18px;
        font-weight: bold;
      }

      .teacher-subject {
        color: #409EFF;
        font-size: 14px;
        margin-bottom: 10px;
      }

      .teacher-stats {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;

        .stat-item {
          display: flex;
          align-items: center;
          font-size: 12px;
          color: #606266;

          i {
            margin-right: 4px;
          }
        }
      }

      .teacher-rating {
        display: flex;
        align-items: center;
        margin-bottom: 10px;

        .el-rate {
          display: inline-block;
        }

        .rating-value {
          margin-left: 5px;
          color: #ff9900;
          font-weight: bold;
        }
      }

      .teacher-intro {
        font-size: 13px;
        color: #606266;
        line-height: 1.5;
        height: 60px;
        overflow: hidden;
      }
    }
  }

  .pagination-container {
    text-align: center;
    margin-top: 20px;
  }
}

@media (max-width: 768px) {
  .teacher-center {
    .page-header {
      flex-direction: column;
      align-items: flex-start;

      .page-title {
        margin-bottom: 15px;
      }

      .search-box {
        width: 100%;
      }
    }

    .filter-section {
      .filter-item {
        .el-radio {
          margin-bottom: 5px;
        }
      }
    }
  }
}
</style> 